import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;


/**
 * AddEditDoctorsOrders
 * 
 * Doctors Orders - This is the treatment plan and consists of:
 *  - Prescription (drugs + instructions for example penicillin 3 times a day)
 *  - Lab Work Performed (blood tests, urine tests, immunizations)
 *  - Follow up instructions (for example come back in 3 days)
 *  - Other instructions (for example, drink fluids, take oatmeal bath, etc)
 * 
 * @author bethanysumner
 *
 * @version $Revision: 1.0 $
 */
public class AddEditDoctorsOrders extends JPanel{
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	/**
	 * Field time.
	 */
	/**
	 * Field date.
	 */
	private String date, time;
	/**
	 * Field txtInstructions.
	 */
	private JTextField txtInstructions;
	/**
	 * Field btnCancel.
	 */
	/**
	 * Field btnSubmit.
	 */
	private JButton btnSubmit, btnCancel;
	/**
	 * Field comboLabWork.
	 */
	/**
	 * Field comboDrug.
	 */
	private JComboBox comboDrug, comboLabWork;
	/**
	 * Field buttonListener.
	 */
	private ButtonListener buttonListener;
	/**
	 * Field treatment.
	 */
	private String[] treatment;
	
	/**
	 * Constructor for AddEditDoctorsOrders.
	 * @param main MainWindow
	 * @param database Database
	 * @param date String
	 * @param time String
	 */
	public AddEditDoctorsOrders(MainWindow main, Database database, String date, String time) {
		
		this.main = main;
		this.database = database;
		this.date = date;
		this.time = time;
		
		createLayout();
		populateFields(); // you know there's a treatment record, worst case the fields are blank
		
	}
	
	/**
	 * Populates the fields on the GUI
	 */
	private void populateFields() {
		
		int perscription = 5, labwork = 6, instructions = 7;
		
		try {
			treatment = database.getTreatment(date, time);
			comboDrug.setSelectedItem(treatment[perscription]);
			comboLabWork.setSelectedItem(treatment[labwork]);
			txtInstructions.setText(treatment[instructions]);
		} catch (SQLException e) {
			main.showError("Problem getting information from databse.");
			e.printStackTrace();
		}
		
	}

	/**
	 * Creates the layout for the GUI
	 */
	private void createLayout(){
		
		setLayout(null);
		
		JLabel lblPerscription = new JLabel("Perscription");
		lblPerscription.setBackground(Color.BLACK);
		lblPerscription.setBounds(26, 21, 130, 16);
		add(lblPerscription);
		
		final JLabel lblDrug = new JLabel("Drug:");
		lblDrug.setBounds(26, 62, 84, 16);
		add(lblDrug);
		
		comboDrug = new JComboBox();
		comboDrug.setBounds(209, 57, 329, 27);
		comboDrug.addItem("None");
		comboDrug.addItem("Amoxicillin");
		comboDrug.addItem("Penicillin");
		comboDrug.addItem("Cefadroxil");
		comboDrug.addItem("Doxepin");
		comboDrug.addItem("Bupropion");
		comboDrug.addItem("Adderall");
		comboDrug.addItem("Ritalin");
		add(comboDrug);
		
		JSeparator separator = new JSeparator();
		separator.setBackground(Color.BLACK);
		separator.setBounds(26, 41, 512, 16);
		add(separator);
		
		final JLabel lblLabWork = new JLabel("Lab Work Performed");
		lblLabWork.setBackground(Color.BLACK);
		lblLabWork.setBounds(26, 118, 130, 16);
		add(lblLabWork);
		
		final JLabel lblLabWork_1 = new JLabel("Lab work:");
		lblLabWork_1.setBounds(26, 162, 84, 16);
		add(lblLabWork_1);
		
		comboLabWork = new JComboBox();
		comboLabWork.setBounds(209, 157, 329, 27);
		comboLabWork.addItem("None");
		comboLabWork.addItem("Urinalysis");
		comboLabWork.addItem("CBC");
		comboLabWork.addItem("Cholesterol Test");
		comboLabWork.addItem("HIV");
		comboLabWork.addItem("Glucose0");
		comboLabWork.addItem("Adrenal");
		comboLabWork.addItem("EKG");
		comboLabWork.addItem("MRI");
		add(comboLabWork);
		
		final JSeparator separator_1 = new JSeparator();
		separator_1.setBackground(Color.BLACK);
		separator_1.setBounds(25, 140, 512, 16);
		add(separator_1);
		
		final JLabel lblInstructions = new JLabel("Instructions");
		lblInstructions.setBounds(26, 219, 84, 16);
		add(lblInstructions);
		
		txtInstructions = new JTextField();
		txtInstructions.setColumns(10);
		txtInstructions.setBounds(26, 251, 512, 34);
		add(txtInstructions);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBackground(Color.BLACK);
		separator_2.setBounds(26, 235, 512, 16);
		add(separator_2);
		
		buttonListener = new ButtonListener();
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(421, 341, 117, 29);
		btnSubmit.addActionListener(buttonListener);
		add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(302, 341, 117, 29);
		btnCancel.addActionListener(buttonListener);
		add(btnCancel);
	}
	
	/**
	 * Submits any changes to the database
	 */
	private void submit(){
		String drug = comboDrug.getSelectedItem().toString();
		String labwork = comboLabWork.getSelectedItem().toString();
		String instructions = txtInstructions.getText();
	
		try {
			database.updateTR_doctorsOrders(date, time, drug, labwork, instructions);
			main.showAffirmation("You doctor's orders have been sucessfully added to the treatment record.");
			main.removeTab(this);
		} catch (SecurityException e) {
			main.showError("Problem updating database.");
			e.printStackTrace();
		} catch (SQLException e) {
			main.showError("Problem updating database.");
			e.printStackTrace();
		}
	}
	
	/**
	 * Cancels any changes and closes the tab
	 */
	private void cancel(){
		main.removeTab(this);
		main.clearMessage();
	}
	
	/**
	 * Action listener for the buttons
	 * @author Bethany Sumner
	 * @version 1.0
	 */
	private class ButtonListener implements ActionListener{
		
		/**
		 * Performs tasks corresponding to the action
		 * @param event The action event
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource().equals(btnSubmit)){
				submit();
			}else if(event.getSource().equals(btnCancel)){
				cancel();
			}
			
		}
		
	}
	
}
